import { isEmpty } from 'lodash-es'

// 列设置 更新 列
export const refreshColumns = (columns: any, newColumns: any) => {
  const originColumnsMap = columns.reduce(
    (prev: any, curr: any, index: number, array: any) => {
      const { key, dataIndex } = curr
      prev[key || dataIndex] = curr
      return prev
    },
    {}
  )
  return newColumns.reduce(
    (prev: any, curr: any, index: number, array: any) => {
      const { key, dataIndex, show, fixed } = curr
      prev.push({ ...originColumnsMap[key || dataIndex], fixed, show })

      return prev
    },
    []
  )
}

// 接口请求数据处理
export const requestParamsFormat = (formData: any) => {
  if (isEmpty(formData)) {
    return formData
  }
  if (typeof formData === 'object' && !Array.isArray(formData)) {
    Object.keys(formData).forEach((item) => {
      if (isEmpty(formData[item]) && typeof formData[item] !== 'number') {
        delete formData[item]
      } else if (typeof formData[item] === 'object') {
        requestParamsFormat(formData[item])
      }
    })
  } else if (Array.isArray(formData)) {
    formData.forEach((el) => {
      if (!isEmpty(el) && typeof el === 'object') {
        requestParamsFormat(el)
      }
    })
  }
  return formData
}
export const priceSwitch = (x: number | string) => {
  // x = String(x)
  // //强制保留两位小数
  // let f = parseFloat(x)
  // if (isNaN(f)) return false
  // f = Math.round(Number(x) * 100) / 100
  // let s = f.toString()
  // let rs = s.indexOf(".")
  // if (rs < 0) {
  //   rs = s.length
  //   s += "."
  // }
  // while (s.length < rs + 1 + 2) {
  //   s += "0"
  // }
  //每三位用一个逗号隔开
  const s = String(x)
  let leftNum = s.split('.')[0]
  const rightNum = s.split('.')[1] ? '.' + s.split('.')[1] : ''
  let result
  //定义数组记录截取后的价格
  const resultArray = []
  if (leftNum.length > 3) {
    let i = true
    while (i) {
      resultArray.push(leftNum.slice(-3))
      leftNum = leftNum.slice(0, leftNum.length - 3)
      if (leftNum.length < 4) {
        i = false
      }
    }
    //由于从后向前截取，所以从最后一个开始遍历并存到一个新的数组，顺序调换
    const sortArray = []
    for (let i = resultArray.length - 1; i >= 0; i--) {
      sortArray.push(resultArray[i])
    }
    result = leftNum + ',' + sortArray.join(',') + rightNum
  } else {
    result = s
  }
  return result
}
// export const priceSwitchUnit = (value: number | string) => {
//   value = Number(value)
//   //小数保留两位，整数不显示小数 Math.round(value*100)/100
//   if (value / 100000000 > 1) {
//     const m = priceSwitch(Math.round((value / 100000000) * 100) / 100)
//     return m + " 亿"
//   } else if (value / 10000000 > 1) {
//     const m = priceSwitch(Math.round((value / 10000000) * 100) / 100)
//     return m + " 千万"
//   } else if (value / 1000000 > 1) {
//     const m = priceSwitch(Math.round((value / 1000000) * 100) / 100)
//     return m + " 百万"
//   } else if (value / 100000 > 1) {
//     const m = priceSwitch(Math.round((value / 100000) * 100) / 100)
//     return m + " 十万"
//   } else if (value / 10000 > 1) {
//     const m = priceSwitch(Math.round((value / 10000) * 100) / 100)
//     return m + " 万"
//   } else {
//     const m = priceSwitch(Math.round(value * 100) / 100)
//     return m
//   }
// }
export const priceSwitchUnit = (value: number | string) => {
  value = Number(value)
  //小数保留两位，整数不显示小数 Math.round(value*100)/100
  if (value / 100000000 >= 1) {
    const m = priceSwitch(Math.floor((value / 100000000) * 100) / 100)
    return m + ' 亿'
  } else if (value / 10000000 >= 1) {
    const m = priceSwitch(Math.floor((value / 10000000) * 100) / 100)
    return m + ' 千万'
  } else if (value / 1000000 >= 1) {
    const m = priceSwitch(Math.floor((value / 1000000) * 100) / 100)
    return m + ' 百万'
  } else if (value / 100000 >= 1) {
    const m = priceSwitch(Math.floor((value / 100000) * 100) / 100)
    return m + ' 十万'
  } else if (value / 10000 >= 1) {
    const m = priceSwitch(Math.floor((value / 10000) * 100) / 100)
    return m + ' 万'
  } else {
    const m = priceSwitch(Math.floor(value * 100) / 100)
    return m
  }
}
export const priceSwitchUnityi = (value: number | string) => {
  value = Number(value)
  if (Number.isNaN(value)) return '--'
  //小数保留两位，整数不显示小数 Math.round(value*100)/100
  if (value / 100000000 >= 1) {
    const m = priceSwitch(Math.floor((value / 100000000) * 100) / 100)
    return m + ' 亿'
  } else {
    const m = priceSwitch(Math.floor(value * 100) / 100)
    return m
  }
}
// 折线图用的，只转换w用的
export const priceSwitchW = (value: number | string) => {
  value = Number(value)
  if (value / 10000 >= 1) {
    const m = priceSwitch(Math.floor((value / 10000) * 100) / 100)
    return m + ' w'
  } else {
    const m = priceSwitch(Math.floor(value * 100) / 100)
    return m
  }
}
// priceSwitchCnW
// 转换万、亿等
export const priceSwitchCnWY = (value: number | string) => {
  value = Number(value)
  if (value == 0 || (value > 0 && value < 10000)) {
    return value + ''
  } else if (value > 10000 && value < 100000000) {
    return (value / 10000).toFixed(2) + '万'
  } else if (value >= 100000000) {
    return (value / 100000000).toFixed(2) + '亿'
  } else {
    return 0
  }
}
//车牌号格式化 车牌号点分隔 车牌号分隔
export const splicePlateNum = (val: string) => {
  if (!val.includes('·')) {
    return val.slice(0, 2) + '·' + val.slice(2, val.length + 1)
  } else {
    return val
  }
}

//手机号分隔 手机号格式化  前3位后4位分隔
export const phoneFormatted = (phone: string) => {
  // if (val) {
  //   return val.replace(/(\d{3})(\d{4})/, "$1 $2 ")
  // } else {
  //   return val
  // }
  // 移除非数字字符
  phone = phone.replace(/[^\d]/g, '')

  if (phone.length === 11) {
    return `${phone.slice(0, 3)} ${phone.slice(3, 7)} ${phone.slice(7)}`
  } else if (phone.length === 8 || phone.length === 9) {
    return `${phone.slice(0, 2)} ${phone.slice(2, 6)} ${phone.slice(6)}`
  } else {
    return phone
  }
}

export function highlightKeyword(text: string, keyword: string) {
  if (!keyword) return false
  text = text.replace('·', '')
  keyword = keyword.replace('.', '')
  const regex = new RegExp(`(${keyword})`, 'gi')
  return regex.test(text)
}

export function maskIdCard(idCard: string) {
  // 正则表达式匹配前6位和后4位
  const reg = /^(.{6})(\d+)(.{4})$/
  // 使用正则表达式进行替换
  const maskedIdCard = idCard.replace(reg, '$1*********$3')
  return maskedIdCard
}
